<?
/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.                  *
 *                                                                    *
 **********************************************************************/

/**
 * Ageset entity manager.
 *
 * @version $Id: TreeMLManager.php,v 1.1 2008/06/02 13:17:30 serikas Exp $
 */

class TreeMLManager extends ORMManager {

	public $table_fields = array(
		'id'        => 'id',
		'parent_id' => 'parent_id',
		'prio'      => 'prio'
	);

	public $lng_table_fields = array(
		'lang_id'   => 'lang_id',
		'id'        => 'id',
		'tree_name' => 'name'
	);
	
	public $lng_table_name = null;

	function __construct($dialog, $jsObjectName = 'd', $table_name = 'tree', $lng_table_name = 'tree_names', $lang_id = 0) {
		parent::__construct($dialog, $jsObjectName, $table_name);
		$this->lng_table_name = $lng_table_name;
		if ($lang_id) {
			$this->lang_id = $lang_id;
		} else {
			$this->lang_id = $_SESSION['lang_id'];
		}
	}

	/**
	 * �������� ������ �� ����
	 *
	 */
	protected function _loadTree() {
		$dialog = $this->dialog;
		$fields  = &$this->table_fields;
		$lng_fields  = &$this->lng_table_fields;
		$arr = array();
		
		foreach ($fields as $fieldID => &$field) {
			array_push($arr, $this->table_name.'.'.$field.' as '.$fieldID);
		}
		
		foreach ($lng_fields as $fieldID => &$field) {
			if (($fieldID != 'lang_id') and  ($fieldID != 'id')) {
				array_push($arr, $this->lng_table_name.'.'.$field.' as '.$fieldID);
			}
		}

		$qs = "select ".join(', ',$arr)." from ".$this->table_name." left join ".$this->lng_table_name." on (".$this->table_name.'.'.$fields['id'].'='.$this->lng_table_name.'.'.$lng_fields['id'].') and ('.$this->lng_table_name.'.'.$lng_fields['lang_id'].'='.$this->lang_id.') order by prio desc,id';
		#print $qs;
		
		#print $qs;

		$this->tree = $this->getArray($qs);
	}	
	
	
	
	
/**#@+
  * ��������� ������ ����������� ��� ��������������
  */
	
	/**
	 * ��������� ������� � ������
	 *
	 */
	function addTreeItem () {
		$dialog = $this->dialog;
		$id_name = $this->id_name;
		$id = $dialog->$id_name;
		$fields = &$this->table_fields;
		$lng_fields = &$this->lng_table_fields;
		
		# Add to main table
		$class_name=ucfirst($this->table_name).'DB';
		$tree = new $class_name;
		$tree->$fields['parent_id']         = $id;
		$this->insert($tree);

		# Add to lng table
		$class_name=ucfirst($this->lng_table_name).'DB';
		$tree_lng = new $class_name;
		$tree_lng->$lng_fields['lang_id']   = $this->lang_id;
		$tree_lng->$lng_fields['id']        = $tree->$fields['id'];
		$tree_lng->$lng_fields['tree_name'] = $dialog->tree_name;
		#print_r($tree_lng);
		$this->insert($tree_lng);
		
		$dialog->returnUrl=$this->path_url.'&'.$this->id_name.'='.$id;
		return $tree->$fields['id'];
	}

	/**
	 * ��������������� ������� ������
	 *
	 */
	function renameTreeItem () {
		$dialog = $this->dialog;
		$id_name = $this->id_name;
		$id = $dialog->$id_name + 0;
		$lng_fields = &$this->lng_table_fields;
		
		$qs="update ".$this->lng_table_name."
			    set ".$lng_fields['tree_name']." = '".$dialog->tree_name."'
			  where ".$lng_fields['id']." = $id
			    and ".$lng_fields['lang_id']." = ".$this->lang_id;
		
		$this->execQuery($qs);
		$dialog->returnUrl=$this->path_url.'&'.$this->id_name.'='.$id;
	}
	
	/**
	 * ������� ������� �� ������
	 *
	 */
	function deleteTreeItem () {
		$dialog = $this->dialog;
		$id_name = $this->id_name;
		$id = $dialog->$id_name + 0;
		$fields = &$this->table_fields;
		
		if (! $this->is_found_parents($id)) {

			$qs="delete from ".$this->table_name."
			      where ".$fields['id']." = $id";
			if (isset($this->tree_idx[$id])) {
				$this->id = $this->tree_idx[$id]['parent_id'];
			} else {
				$this->id = $this->_get_parent_by_id_sql($id);
			}
			$this->execQuery($qs);
			
			if ($this->id == 0)  {
				$add = '&'.$this->set_root_tree_name.'=1';
			}
		} else {
			$dialog->valid = false;
			$dialog->found_parents = true;
		}
		
		$dialog->returnUrl=$this->path_url.'&'.$this->id_name.'='.$this->id.$add;
	}
	
/**#@-*/
	
	
}
?>